﻿using System.Text;
using RAP.Framework.Libary.DomTemplateBuilder.Element.StringActions;
using RAP.Framework.Libary.DomTemplateBuilder.Interface;

namespace RAP.Framework.Libary.DomTemplateBuilder.Handler.StringActions
{
	/// <summary>
	/// 字符串替换Handler
	/// </summary>
	public class StringReplaceHandler : StringActionElementHandler
    {
        public new StringReplace Element
        {
            get
            {
                return base.Element as StringReplace;
            }
        }

        public override void DoRender ( StringBuilder writer, object rootParamObject, object parentParamObject, ITemplateRenderContext context )
        {
			var exp = this.GetPropertyValue ( rootParamObject, parentParamObject, context ) as string;
            if ( string.IsNullOrEmpty ( exp ) )
            {
                return;
            }

			var find = ExpressionUtils.GetFieldValue 
            (
                this.Element.Find
                , rootParamObject
                , parentParamObject
                , context
                , ( ) => { return this.Element.Find; } 
            ) as string;
            if ( string.IsNullOrEmpty ( find ) )
            {
                return;
            }
			var replace = ExpressionUtils.GetFieldValue 
            (
                this.Element.Replace
                , rootParamObject
                , parentParamObject
                , context
                , ( ) => { return this.Element.Replace; } 
            ) as string;

            exp = exp.Replace ( find, replace );
			writer.Append ( exp );
			//PropertyElementHandler.setFieldValue ( this.GetPropertyName ( rootParamObject, parentParamObject, context ), exp, rootParamObject, parentParamObject, context );
        }
    }
}
